package com.sams.service;

import com.sams.dao.MedicationHistoryMapper;
import com.sams.dao.ScoreMapper;
import com.sams.pojo.MedicalHistory;
import com.sams.pojo.PageBean;
import com.sams.pojo.Query1;
import com.sams.pojo.Student_score_subject;
import com.sams.util.SqlSF_tools;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class MedicalHistoryService {

    SqlSessionFactory sqlSessionFactory = SqlSF_tools.getSqlSessionFactory();
    SqlSession sqlSession;
    public boolean addMedicalHistory(MedicalHistory medicalHistory) {
        sqlSession = sqlSessionFactory.openSession(true);
        MedicationHistoryMapper mapper = sqlSession.getMapper(MedicationHistoryMapper.class);
        int addscore = mapper.addMedicalHistory(medicalHistory);
        if (addscore != 0 && medicalHistory != null) {
            sqlSession.close();
            return true;
        } else {
            sqlSession.close();
            return false;
        }
    }

    public PageBean<MedicalHistory> selectbyPage(int currentPage, int pageSize, Query1 query1) {
        sqlSession = sqlSessionFactory.openSession(true);
        MedicationHistoryMapper mapper = sqlSession.getMapper(MedicationHistoryMapper.class);
        /*计算*/
        int begin = (currentPage - 1) * pageSize;
        int size = pageSize;
        List<MedicalHistory> rows = mapper.selectBypage(begin, size,query1);
        int totalCount = mapper.selectTotalCount();
        /*封装成pagebean对象*/
        PageBean<MedicalHistory> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);
        sqlSession.close();
        return pageBean;
    }

    public boolean updatedata(MedicalHistory medicalHistory) {
        sqlSession = sqlSessionFactory.openSession(true);
        MedicationHistoryMapper mapper = sqlSession.getMapper(MedicationHistoryMapper.class);
        int updateddata = mapper.updateddata(medicalHistory);
        if (updateddata != 0) {
            sqlSession.close();
            return true;
        } else {
            sqlSession.close();
            return false;
        }
    }

    public boolean deletedata(String deleteStudentid) {
        sqlSession = sqlSessionFactory.openSession(true);
        MedicationHistoryMapper mapper = sqlSession.getMapper(MedicationHistoryMapper.class);
        int deletedata = mapper.deletedata(deleteStudentid);
        if (deletedata != 0) {
            sqlSession.close();
            return true;
        } else {
            sqlSession.close();
            return false;
        }
    }
}
